# coding=utf-8
import datetime
from utils import db


class Hosts(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    hostname = db.Column(db.String(50))
    ipaddr = db.Column(db.String(50))
    Groups = db.Column(db.String(50))
    Desc = db.Column(db.String(50))
    username = db.Column(db.String(50))
    passwd = db.Column(db.String(50))
    port = db.Column(db.String(50))
    check = db.Column(db.String(10), default="off")

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class projects(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    project = db.Column(db.String(20))
    repo = db.Column(db.String(100))
    env = db.Column(db.String(100))
    path = db.Column(db.String(200))
    zip = db.Column(db.String(20))
    servers = db.Column(db.String(200))


class DeployTask(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    uid = db.Column(db.String(100), primary_key=True)
    # UID标识如： gw-dev-2010
    project = db.Column(db.String(100))
    tag = db.Column(db.String(20))
    status = db.Column(db.String(20), default="待发布")
    before_download = db.Column(db.String(3000))
    after_download = db.Column(db.String(3000))
    before_deploy_sc = db.Column(db.String(3000))
    after_deploy_sc = db.Column(db.String(3000))
    versioninfo = db.Column(db.String(1000))


class scTemp(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    title = db.Column(db.String(100))
    scripts = db.Column(db.String(500))
    desc = db.Column(db.String(100))


class Logs(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    operator = db.Column(db.String(50))
    uid = db.Column(db.String(100), primary_key=True)
    types = db.Column(db.String(50))
    runtime = db.Column(db.DateTime, default=datetime.datetime.now())
    script = db.Column(db.String(2000))
    result = db.Column(db.String(2000))


class middle(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    source = db.Column(db.String(200))
    desc = db.Column(db.String(200))
    type = db.Column(db.String(30))


class Node(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    task = db.Column(db.String(50))  # 发布 任务
    text = db.Column(db.String(50))  # 节点文字
    status = db.Column(db.String(50), default='lightgray')  # 节点状态
    parent = db.Column(db.String(50), nullable=True)  # 父接点
    servers = db.Column(db.String(50), nullable=True)

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class sysoptions(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    values = db.Column(db.String(20))
    types = db.Column(db.String(50))
    desc = db.Column(db.String(200))
